package com.example.demo.event;

import com.example.demo.entity.LogApi;
import com.example.demo.service.ILogApiService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Map;


/**
 * 异步监听日志事件
 *
 * @author cici
 */
@Slf4j
@AllArgsConstructor
public class ApiLogListener {

	private final ILogApiService logService;


	@Async
	@Order
	@EventListener(ApiLogEvent.class)
	public void saveApiLog(ApiLogEvent event) {
		Map<String, Object> source = (Map<String, Object>) event.getSource();
		LogApi logApi = (LogApi) source.get("log");
		logService.save(logApi);
	}

}
